package cn.ztydata.teachingsystem.heaven.dao;

import cn.ztydata.teachingsystem.heaven.cons.ChapterOverview;
import cn.ztydata.teachingsystem.heaven.cons.MsgTemplate;
import cn.ztydata.teachingsystem.heaven.entity.ChapterProfile;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.MessageFormat;
import java.util.Map;

/**
 * 章节概况dao类
 * Created by chenxin on 2014/12/16.
 */
@Repository
public class ChapterOverviewDao extends BaseDao {
    /**
     * 更新章节概况内容
     * @param nodeId 章节ID
     * @param type 概况类型
     * @param content 概况内容
     * @return boolean
     *
     * @author cx
     * @since 2014-12-16
     */
    public boolean updateContent(final long nodeId, final int type, final String content) {
        String field = judgeFied(type);
        if (field == null) {
            log.debug("概况字段为空：field == null");
            return false;
        }

        String sql = "UPDATE t_chapter_overview SET " + field + "=:content WHERE iChapterId=:nodeId";

        SqlParameterSource sps = new MapSqlParameterSource().addValue("field", field)
                .addValue("content", content)
                .addValue("nodeId", nodeId);

        log.debug("更新章节概况内容结果");
        return namedParameterJdbcTemplate.update(sql, sps) == 1;
    }

    /**
     * 查询指定类型的章节概况
     * @param nodeId 节点ID
     * @param type 概况类型
     * @return String
     *
     * @author cx
     * @since 2014-12-16
     */
    public ChapterProfile getContent(final long nodeId, final int type) {
        //文本内容
        final String field = judgeFied(type);
        //下载url
        //final String downloadFied = judgeDownloadFied(type);
        //显示url
        //final String showField = judgeShowFied(type);

        if (field == null) {
            log.debug("判断概况字段为空：field == null");
            return null;
        }

        //String sql = "SELECT " + field + "," + downloadFied + "," + showField + " FROM t_chapter_overview WHERE iChapterId=:nodeId";

        String sql = "SELECT " + field + " FROM t_chapter_overview WHERE iChapterId=:nodeId";

        try {
            SqlParameterSource sps = new MapSqlParameterSource().addValue("field", field).addValue("nodeId", nodeId);

            log.debug("正常返回查询到的指定类型的章节概况");

            return namedParameterJdbcTemplate.queryForObject(sql, sps, new RowMapper<ChapterProfile>() {
                @Override
                public ChapterProfile mapRow(ResultSet resultSet, int i) throws SQLException {
                    ChapterProfile profile = new ChapterProfile();

                    profile.setNodeId(nodeId);
                    profile.setType(type);
                    profile.setContent(resultSet.getString(field));

                    return profile;
                }
            });
        } catch (EmptyResultDataAccessException ex) {
            log.debug("查询指定类型的章节概况失败");
            log.warn(MessageFormat.format(MsgTemplate.RECORD_NOT_FOUND, "nodeId", nodeId));
            return null;
        }
    }

    /**
     * 获取概况字段是否为空
     *
     * @author chenxin
     * @since 2014/12/17
     */
    public Map<String, Object> getForNull(final long nodeId) {
        StringBuilder sql = new StringBuilder("SELECT ");

        sql.append("sTeachingDemands IS NOT NULL && TRIM(sTeachingDemands)<>'' AS '1'")
                .append(",sEvaluation IS NOT NULL && TRIM(sEvaluation)<>'' AS '2'")
                .append(",sStudyGoal IS NOT NULL && TRIM(sStudyGoal)<>'' AS '3'")
                .append(",sModuleGuidance IS NOT NULL && TRIM(sModuleGuidance)<>'' AS '4'")
                .append(",sForcusGuidance IS NOT NULL && TRIM(sForcusGuidance)<>'' AS '5'")
                .append(",sTeachingPlan IS NOT NULL && TRIM(sTeachingPlan)<>'' AS '6'")
                .append(",sReference IS NOT NULL && TRIM(sReference)<>'' AS '7'")
                .append(",sTextbookContent IS NOT NULL && TRIM(sTextbookContent)<>'' AS '8'");

        sql.append(" FROM t_chapter_overview WHERE iChapterId=:nodeId");

        try {
            log.debug("正常返回获取概况字段是否为空");
            return namedParameterJdbcTemplate.queryForMap(sql.toString(), new MapSqlParameterSource().addValue("nodeId", nodeId));
        } catch (EmptyResultDataAccessException ex) {
            log.debug("获取概况字段是否为空失败");
            log.warn(MessageFormat.format(MsgTemplate.RECORD_NOT_FOUND, "nodeId", nodeId));
            return null;
        }
    }

    /**
     * 删除章节概况
     *
     * @author chenxin
     * @since 2014/12/19
     */
    public boolean deleteById(final long nodeId) {
        String sql = "DELETE FROM t_chapter_overview WHERE iChapterId=:nodeId";

        return namedParameterJdbcTemplate.update(sql, new MapSqlParameterSource().addValue("nodeId", nodeId)) == 1;
    }

    /**
     * 新增保存章节概况
     *
     * @author chenxin
     * @since 2014/12/19
     */
    public boolean save(final long nodeId) {
        String sql = "INSERT INTO t_chapter_overview(iChapterId) VALUES(:nodeId)";

        log.debug("新增保存章节概况");
        return namedParameterJdbcTemplate.update(sql, new MapSqlParameterSource().addValue("nodeId", nodeId)) == 1;
    }

    /**
     * 判断概况字段
     * @param type 概况类型
     * @return String
     *
     * @author fanruiping
     * @since 2015-02-05
     */
    private String judgeFied(final int type) {
        switch (type) {
            case ChapterOverview.TEACHING_DEMANDS: //教学要求
                log.debug("判断概况字段教学要求type等于ChapterOverview.TEACHING_DEMANDS");
                return "sTeachingDemands";
            case ChapterOverview.EVALUATION: //评价考核
                log.debug("判断概况字段评价考核type等于ChapterOverview.EVALUATION");
                return "sEvaluation";
            case ChapterOverview.STUDY_GOAL: //学习目标
                log.debug("判断概况字段学习目标type等于ChapterOverview.STUDY_GOAL");
                return "sStudyGoal";
            case ChapterOverview.MODULE_GUIDANCE: //模块导学
                log.debug("判断概况字段模块导学type等于ChapterOverview.MODULE_GUIDANCE");
                return "sModuleGuidance";
            case ChapterOverview.FORCUS_GUIDANCE: //重点难点辅导
                log.debug("判断概况字段重点难点辅导type等于ChapterOverview.FORCUS_GUIDANCE");
                return "sForcusGuidance";
            case ChapterOverview.TEACHING_PLAN: //教学设计
                log.debug("判断概况字段教学设计type等于ChapterOverview.TEACHING_PLAN");
                return "sTeachingPlan";
            case ChapterOverview.REFERENCE: //参考资料目录
                log.debug("判断概况字段参考资料目录type等于ChapterOverview.REFERENCE");
                return "sReference";
            case ChapterOverview.TEXTBOOK_CONTENT: //教材内容
                log.debug("判断概况字段教材内容type等于ChapterOverview.TEXTBOOK_CONTENT");
                return "sTextbookContent";
            default:
                log.debug("错误的概况类型");
                log.warn("错误的概况类型:type->" + type);
                return null;
        }
    }
}
